package com.lc.luzhengtang.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.ClassficationAdapter;
import com.lc.luzhengtang.bean.ClassFication;
import com.lc.luzhengtang.conn.ConnGoodstype;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.zcx.helper.http.AsyCallBack;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/5/2.
 */

public class ClassificationFragment extends BaseFragment3 implements View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private Context context;
    ClassficationAdapter newsAdapter;
    private int currentPage = 1;

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onRefresh() {

        new ConnGoodstype(currentPage + "", new AsyCallBack<ClassFication>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                //  showLoading();

            }

            @Override
            public void onSuccess(String toast, int type, Object o, ClassFication classFication) throws Exception {
                super.onSuccess(toast, type, o, classFication);
                  /*  List<GankModel> results = response.body().results;
                    if (results != null) {
                        currentPage = 2;
                        newsAdapter.setNewData(results);
                    }*/
                List<ClassFication.DataBeanX.DataBean> results = classFication.getData().getData();
                if (results != null) {
                    currentPage = 1;
                    newsAdapter.setNewData(results);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // dismissLoading();
                setRefreshing(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                //可能需要移除之前添加的布局
//                newsAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
                newsAdapter.setEnableLoadMore(false);
            }
        }).execute(getActivity(), false);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onLoadMoreRequested() {
        new ConnGoodstype(currentPage + "", new AsyCallBack<ClassFication>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                //  showLoading();
                newsAdapter.setEnableLoadMore(true);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ClassFication classFication) throws Exception {
                super.onSuccess(toast, type, o, classFication);

                List<ClassFication.DataBeanX.DataBean> results = classFication.getData().getData();
                if (results != null && results.size() > 0) {
                    currentPage++;
                    newsAdapter.setNewData(results);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    newsAdapter.loadMoreComplete();
                    View noDataView = inflater.inflate(R.layout.item_no_data, (ViewGroup) recyclerView.getParent(), false);
                    newsAdapter.addFooterView(noDataView);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // dismissLoading();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                //可能需要移除之前添加的布局
                newsAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);

            }
        }).execute(getActivity(), false);
    }

    @Override
    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new FullyGridLayoutManager(getActivity(), 2));

        newsAdapter = new ClassficationAdapter(null);
        newsAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        newsAdapter.isFirstOnly(false);
        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider);
 /*       recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 1));

      */
        recyclerView.setAdapter(newsAdapter);
        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        newsAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // TODO: inflate a fragment view
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
        unbinder = ButterKnife.bind(this, view);
        initData();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    /** 上拉加载 */
   /* @Override
    public void onLoadMoreRequested() {
        OkGo.<GankResponse<List<GankModel>>>get(url + currentPage)//
                .cacheMode(CacheMode.NO_CACHE)       //上拉不需要缓存
                .execute(new NewsCallback<GankResponse<List<GankModel>>>() {
                    @Override
                    public void onSuccess(Response<GankResponse<List<GankModel>>> response) {
                        List<GankModel> results = response.body().results;
                        if (results != null && results.size() > 0) {
                            currentPage++;
                            newsAdapter.addData(results);
                        } else {
                            //显示没有更多数据
                            newsAdapter.loadComplete();
                            View noDataView = inflater.inflate(R.layout.item_no_data, (ViewGroup) recyclerView.getParent(), false);
                            newsAdapter.addFooterView(noDataView);
                        }
                    }

                    @Override
                    public void onError(Response<GankResponse<List<GankModel>>> response) {
                        //显示数据加载失败,点击重试
                        newsAdapter.showLoadMoreFailedView();
                        //网络请求失败的回调,一般会弹个Toast
                        showToast(response.getException().getMessage());
                    }
                });
    }
*/
}
